using HKB.Common;
using HKB.Data.Dal.S_DeliverTypeTableAdapters;
using System;
using System.Collections;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Xml;
namespace HKB.Web
{
	public class Shop_DeliverType
	{
		private int m_Index = 0;
		private Conn conn = null;
		private static readonly HLShop_DeliverTypeTableAdapter Adapter = new HLShop_DeliverTypeTableAdapter();
		private Pagination _pageHand = null;
		protected Pagination pageHand
		{
			get
			{
				Pagination pageHand;
				if (this._pageHand == null)
				{
					this._pageHand = new Pagination();
					this._pageHand.SQL_CloseTablePre = false;
					this._pageHand.SQL_Fields = "*,(SELECT UserName From HL_User Where HL_User.UserID=HLShop_DeliverType.UserID) AS UserName";
					this._pageHand.P_Object = "物流方式";
					pageHand = this._pageHand;
				}
				else
				{
					pageHand = this._pageHand;
				}
				return pageHand;
			}
		}
		public Shop_DeliverType()
		{
			this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		}
		public Shop_DeliverType(Conn _conn)
		{
			this.conn = _conn;
		}
		public DataTable GetDeliverTypeByUserIDTypeIDStatus(int UserID, int Typeid, int Status)
		{
			return Shop_DeliverType.Adapter.GetDeliverTypeByUserIDTypeIDStatusDisabled(new int?(Typeid), new int?(UserID), new int?(1), new int?(0));
		}
		public DataTable GetDeliverTypeByUserIDTypeIDStatusDisabled(int TypeID, int UserID, int Status, int IsDisabled)
		{
			return Shop_DeliverType.Adapter.GetDeliverTypeByUserIDTypeIDStatusDisabled(new int?(TypeID), new int?(UserID), new int?(Status), new int?(IsDisabled));
		}
		public string MakeDeliverTemplate()
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("<?xml version=\"1.0\" standalone=\"yes\"?>");
			stringBuilder.Append("<root>");
			stringBuilder.Append(string.Concat(new string[]
			{
				"<project name=\"",
				HttpContext.Current.Request.Form["deliver_project_name"],
				"\" lastupdate=\"",
				DateTime.Now.ToString(),
				"\">"
			}));
			string text = string.Empty;
			string text2 = string.Empty;
			string text3 = string.Empty;
			string text4 = string.Empty;
			string text5 = string.Empty;
			string text6 = string.Empty;
			string text7 = string.Empty;
			string text8 = string.Empty;
			string[] allKeys = HttpContext.Current.Request.Form.AllKeys;
			for (int i = 0; i < allKeys.Length; i++)
			{
				string text9 = allKeys[i];
				text5 += this.GetItemStr("ord_item", text9);
				text7 += this.GetItemStr("post_fast_item", text9);
				text6 += this.GetItemStr("corp_fast_item", text9);
				text8 += this.GetItemStr("ems_item", text9);
				string text10 = text9;
				if (text10 != null)
				{
					if (!(text10 == "has_ordinary"))
					{
						if (!(text10 == "has_corp_fast"))
						{
							if (!(text10 == "has_post_fast"))
							{
								if (text10 == "has_ems")
								{
									text4 = string.Concat(new string[]
									{
										"<deliver name=\"EMS\" default=\"",
										HttpContext.Current.Request.Form["ems_default"],
										"\" defIncrement=\"",
										HttpContext.Current.Request.Form["ems_default_increment"],
										"\">"
									});
								}
							}
							else
							{
								text3 = string.Concat(new string[]
								{
									"<deliver name=\"邮政快递\" default=\"",
									HttpContext.Current.Request.Form["postfast_default"],
									"\" defIncrement=\"",
									HttpContext.Current.Request.Form["postfast_increment"],
									"\">"
								});
							}
						}
						else
						{
							text2 = string.Concat(new string[]
							{
								"<deliver name=\"其它快递\" default=\"",
								HttpContext.Current.Request.Form["corpfast_default"],
								"\" defIncrement=\"",
								HttpContext.Current.Request.Form["corpfast_increment"],
								"\">"
							});
						}
					}
					else
					{
						text = string.Concat(new string[]
						{
							"<deliver name=\"平邮\" default=\"",
							HttpContext.Current.Request.Form["ordpost_default"],
							"\" defIncrement=\"",
							HttpContext.Current.Request.Form["ordpost_increment"],
							"\">"
						});
					}
				}
			}
			if (!string.IsNullOrEmpty(text))
			{
				text = text + text5 + "</deliver>";
				stringBuilder.Append(text);
			}
			if (!string.IsNullOrEmpty(text2))
			{
				text2 = text2 + text6 + "</deliver>";
				stringBuilder.Append(text2);
			}
			if (!string.IsNullOrEmpty(text3))
			{
				text3 = text3 + text7 + "</deliver>";
				stringBuilder.Append(text3);
			}
			if (!string.IsNullOrEmpty(text4))
			{
				text4 = text4 + text8 + "</deliver>";
				stringBuilder.Append(text4);
			}
			stringBuilder.Append("</project></root>");
			return stringBuilder.ToString();
		}
		protected string GetItemStr(string ItemType, string s)
		{
			string result = string.Empty;
			if (-1 != s.IndexOf(ItemType))
			{
				this.m_Index++;
				switch (this.m_Index)
				{
				case 1:
					result = "<item><area>" + HttpContext.Current.Request.Form[s] + "</area>";
					break;
				case 2:
					result = "<price>" + HttpContext.Current.Request.Form[s] + "</price>";
					break;
				case 3:
					result = "<increment>" + HttpContext.Current.Request.Form[s] + "</increment></item>";
					this.m_Index = 0;
					break;
				}
			}
			return result;
		}
		public bool UpDateDeliverType(int TypeID, bool ISDisable, string TypeName, decimal Charge, int ChargeType, string Intro)
		{
			string deliverAraa = this.MakeDeliverTemplate();
			int num = Shop_DeliverType.Adapter.UpdateDeliverType(new bool?(ISDisable), TypeName, new decimal?(Charge), Intro, new int?(ChargeType), deliverAraa, TypeID);
			return num == 1;
		}
		public bool InsertDeliverType(bool ISDisable, string TypeName, decimal Charge, int ChargeType, string Intro, int UserID, bool IsDefault)
		{
			string deliverAraa = this.MakeDeliverTemplate();
			int num = Shop_DeliverType.Adapter.InsertDeliverType(new int?(UserID), TypeName, Intro, new int?(0), new bool?(true), new bool?(false), new decimal?(0m), deliverAraa);
			return num == 1;
		}
		public void SetDeliverTypeAsDefault(int TypeID)
		{
			Shop_DeliverType.Adapter.SetDeliverTypeAsDefault(new int?(TypeID));
		}
		public DataTable GetDeliverTypeByUserIDTypeName(int UserID, string TypeName)
		{
			return Shop_DeliverType.Adapter.GetGetDeliverTypeByUserIDTypeName(new int?(UserID), TypeName);
		}
		public bool DeleteDeliver(int TypeID, int UserID)
		{
			return Shop_DeliverType.Adapter.DeleteDeliverType(TypeID, new int?(UserID)) == 1;
		}
		public bool DeleteDeliver(int TypeID)
		{
			string sql = "delete from HLShop_DeliverType where TypeID=" + TypeID;
			int num = this.conn.Execute(sql);
			return num > 0;
		}
		public DataTable GetDeliverTypeByID(int typeID)
		{
			return this.conn.GetDataTable("SELECT * FROM HLShop_DeliverType WHERE [TypeID] = " + typeID);
		}
		public DataTable GetDeliverTypeByUserIDTypeID(int UserID, string TypeID)
		{
			DataTable result;
			if (UserID != 0 && TypeID != "")
			{
				if (TypeID.Contains("|"))
				{
					result = Shop_DeliverType.Adapter.GetDeliverTypeByUserIDTypeIDArrayNotDisabled(new int?(UserID), TypeID);
				}
				else
				{
					result = Shop_DeliverType.Adapter.GetDeliverTypeByUserIDTypeIDNotDisable(new int?(UserID), Convert.ToInt32(TypeID));
				}
			}
			else
			{
				result = null;
			}
			return result;
		}
		public string GetAuctionDeliverByUserIDTypeID(int UserID, int TypeID)
		{
			string result;
			if (UserID < 0 || TypeID < 0)
			{
				result = "";
			}
			else
			{
				DataTable deliverTypeByUserIDTypeIDNotDisable = Shop_DeliverType.Adapter.GetDeliverTypeByUserIDTypeIDNotDisable(new int?(UserID), TypeID);
				if (deliverTypeByUserIDTypeIDNotDisable != null && deliverTypeByUserIDTypeIDNotDisable.Rows.Count > 0)
				{
					string text = deliverTypeByUserIDTypeIDNotDisable.Rows[0]["DeliverAraa"].ToString();
					if (!string.IsNullOrEmpty(text) && text != "")
					{
						XmlDocument xmlDocument = new XmlDocument();
						xmlDocument.LoadXml(text);
						XmlNodeList xmlNodeList = xmlDocument.SelectNodes("root/project/deliver");
						StringBuilder stringBuilder = new StringBuilder();
						stringBuilder.Append("<div id='area_table'></div>" + Environment.NewLine);
						stringBuilder.Append("<script type='text/javascript'>" + Environment.NewLine);
						stringBuilder.Append("   var deliver_area=new Array();" + Environment.NewLine);
						int num = 0;
						foreach (XmlNode xmlNode in xmlNodeList)
						{
							stringBuilder.AppendFormat("deliver_area[{0}]=[", num.ToString());
							stringBuilder.AppendFormat("['{0}','{1}',{2},{3}]", new object[]
							{
								xmlNode.Attributes["name"].Value,
								Language.Get("Shop/Deliver/All"),
								xmlNode.Attributes["default"].Value,
								xmlNode.Attributes["defIncrement"].Value
							});
							if (xmlNode.HasChildNodes)
							{
								XmlNodeList xmlNodeList2 = xmlNode.SelectNodes("item");
								foreach (XmlNode xmlNode2 in xmlNodeList2)
								{
									stringBuilder.AppendFormat(",['{0}',{1},{2}]", xmlNode2.SelectSingleNode("area").InnerXml, xmlNode2.SelectSingleNode("price").InnerXml, xmlNode2.SelectSingleNode("increment").InnerXml);
								}
							}
							stringBuilder.Append("];" + Environment.NewLine);
							num++;
						}
						stringBuilder.Append("</script>" + Environment.NewLine);
						result = stringBuilder.ToString();
					}
					else
					{
						result = "<script type='text/javascript'> var deliver_area=new Array();</script>";
					}
				}
				else
				{
					result = "<script type='text/javascript'> var deliver_area=new Array();</script>没有设置运送方式！";
				}
			}
			return result;
		}
		public bool CheckSalerDeliverTypeLimit(int LimitNum, int SalerID)
		{
			string text = Convert.ToString(Shop_DeliverType.Adapter.CountDeliverTypeBySalerID(new int?(SalerID)));
			bool result;
			if (string.IsNullOrEmpty(text))
			{
				result = true;
			}
			else
			{
				int num = int.Parse(text);
				result = (num < LimitNum);
			}
			return result;
		}
		public DataTable GetDeliverTypeByUserID(int userID)
		{
			string sql = string.Format("SELECT * FROM HLSHOP_DeliverType WHERE UserID='{0}'", userID);
			return this.conn.GetDataTable(sql);
		}
		public DataTable GetDeliverTypeByUserID(int userID, bool isdefault)
		{
			DataTable dataTable = new DataTable();
			DataTable result;
			if (isdefault)
			{
				string sql = string.Format("SELECT * FROM HLSHOP_DeliverType WHERE UserID={0} AND isDefault={1}", userID, 1);
				result = this.conn.GetDataTable(sql);
			}
			else
			{
				result = dataTable;
			}
			return result;
		}
		public string GetPagePanel(string linkHead)
		{
			this.pageHand.P_LinkHead = linkHead;
			return this.pageHand.ShowPagePanel();
		}
		public DataTable GetAllDeliverType(int indexpage, int maxperpage, string wherestr, string orderbystr)
		{
			return this.pageHand.GetDataTable("HLShop_DeliverType", indexpage, maxperpage, "TypeID", wherestr, orderbystr, this.conn, null);
		}
		public DataTable GetAllDeliverType(int indexpage, int maxperpage, string wherestr, string orderbystr, params IDbDataParameter[] param)
		{
			return this.pageHand.GetDataTable("HLShop_DeliverType", indexpage, maxperpage, "TypeID", wherestr, orderbystr, this.conn, param);
		}
		public bool SetDisenabled(bool Disenabled, int DeliverTypeID)
		{
			int num;
			if (Disenabled)
			{
				num = this.conn.Execute("Update HLShop_DeliverType set IsDisabled = 1  Where TypeID = " + DeliverTypeID);
			}
			else
			{
				num = this.conn.Execute("Update HLShop_DeliverType set IsDisabled = 0  Where TypeID = " + DeliverTypeID);
			}
			return num > 0;
		}
		public decimal[] GetPrice(int typeID, string DeliverName, string city)
		{
			decimal[] array = new decimal[]
			{
				0.00m,
				0.00m
			};
			DataTable dataTable = this.conn.GetDataTable("Select DeliverAraa from HLShop_DeliverType Where TypeID=" + typeID);
			decimal[] result;
			if (dataTable.Rows.Count <= 0)
			{
				result = array;
			}
			else
			{
				string xml = dataTable.Rows[0]["DeliverAraa"].ToString();
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.LoadXml(xml);
				XmlElement documentElement = xmlDocument.DocumentElement;
				XmlNode xmlNode = documentElement.SelectSingleNode("project");
				foreach (XmlNode xmlNode2 in xmlNode.ChildNodes)
				{
					if (xmlNode2.Attributes["name"].Value == DeliverName)
					{
						if (!string.IsNullOrEmpty(city) && city != "")
						{
							foreach (XmlNode xmlNode3 in xmlNode2.ChildNodes)
							{
								string innerText = xmlNode3.SelectSingleNode("area").InnerText;
								if (innerText.Contains(city))
								{
									array[0] = Convert.ToDecimal(xmlNode3.SelectSingleNode("price").InnerText);
									array[1] = Convert.ToDecimal(xmlNode3.SelectSingleNode("increment").InnerText);
									break;
								}
							}
							break;
						}
						array[0] = Convert.ToDecimal(xmlNode2.Attributes["default"].Value);
						array[1] = Convert.ToDecimal(xmlNode2.Attributes["defIncrement"].Value);
						string text = xmlNode2.InnerXml.ToString();
						break;
					}
				}
				result = array;
			}
			return result;
		}
		public DataTable GetTableType(int typeID)
		{
			DataTable dataTable = null;
			DataTable dataTable2 = this.conn.GetDataTable("Select DeliverAraa,TypeName from HLShop_DeliverType Where TypeID=" + typeID);
			if (dataTable2.Rows.Count > 0)
			{
				string xml = dataTable2.Rows[0]["DeliverAraa"].ToString();
				string value = dataTable2.Rows[0]["TypeName"].ToString();
				dataTable = new DataTable();
				dataTable.Columns.Add("TypeName");
				dataTable.Columns.Add("name");
				dataTable.Columns.Add("default");
				dataTable.Columns.Add("defIncrement");
				dataTable.Columns.Add("area");
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.LoadXml(xml);
				XmlElement documentElement = xmlDocument.DocumentElement;
				XmlNode xmlNode = documentElement.SelectSingleNode("project");
				foreach (XmlNode xmlNode2 in xmlNode.ChildNodes)
				{
					string value2 = xmlNode2.Attributes["name"].Value;
					DataRow dataRow = dataTable.NewRow();
					dataRow["TypeName"] = value;
					dataRow["name"] = value2;
					dataRow["default"] = xmlNode2.Attributes["default"].Value;
					dataRow["defIncrement"] = xmlNode2.Attributes["defIncrement"].Value;
					dataRow["area"] = "";
					dataTable.Rows.Add(dataRow);
					if (xmlNode2.HasChildNodes)
					{
						XmlNodeList xmlNodeList = xmlNode2.SelectNodes("item");
						for (int i = 0; i < xmlNodeList.Count; i++)
						{
							string value3 = "";
							string value4 = "";
							string value5 = "";
							foreach (XmlNode xmlNode3 in xmlNodeList[i].ChildNodes)
							{
								if (xmlNode3.Name == "area")
								{
									value3 = xmlNode3.InnerText;
								}
								else
								{
									if (xmlNode3.Name == "price")
									{
										value4 = xmlNode3.InnerText;
									}
									else
									{
										if (xmlNode3.Name == "increment")
										{
											value5 = xmlNode3.InnerText;
										}
									}
								}
							}
							if (!string.IsNullOrEmpty(value3) && !string.IsNullOrEmpty(value4) && !string.IsNullOrEmpty(value5))
							{
								DataRow dataRow2 = dataTable.NewRow();
								dataRow2["TypeName"] = value;
								dataRow2["name"] = value2;
								dataRow2["default"] = value4;
								dataRow2["defIncrement"] = value5;
								dataRow2["area"] = value3;
								dataTable.Rows.Add(dataRow2);
							}
						}
					}
				}
			}
			return dataTable;
		}
		public string SetDeliverChargeContentDetail(int deliverTypeID, string deliverName, string arriveArea, decimal defaultPrice, decimal deincrementPrice)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("deliverTypeID#" + deliverTypeID + "$");
			stringBuilder.Append("deliverName#" + deliverName + "$");
			stringBuilder.Append("arriveArea#" + arriveArea + "$");
			stringBuilder.Append("defaultPrice#" + defaultPrice + "$");
			stringBuilder.Append("deincrementPrice#" + deincrementPrice + "$");
			return stringBuilder.ToString();
		}
		public static string GetDeliverChargeContentDetailByName(string TitleName, string[] ContentArray)
		{
			string result;
			if (ContentArray != null && ContentArray.Length > 0)
			{
				for (int i = 0; i < ContentArray.Length; i++)
				{
					string[] array = ContentArray[i].Split(new char[]
					{
						'#'
					});
					if (array[0] == TitleName)
					{
						result = array[1];
						return result;
					}
				}
			}
			result = "";
			return result;
		}
		public ArrayList GetDeliverFashionName(int Delive_TypeID)
		{
			ArrayList arrayList = null;
			DataTable tableType = this.GetTableType(Delive_TypeID);
			if (tableType != null)
			{
				if (tableType.Rows.Count > 0)
				{
					string text = "";
					foreach (DataRow dataRow in tableType.Rows)
					{
						string text2 = "";
						if (dataRow["name"] != DBNull.Value)
						{
							text2 = dataRow["name"].ToString();
						}
						if (!string.IsNullOrEmpty(text2))
						{
							string str = string.Concat(new object[]
							{
								Delive_TypeID,
								"|",
								text2,
								","
							});
							text += str;
						}
					}
					if (!string.IsNullOrEmpty(text))
					{
						text = text.Substring(0, text.Length - 1);
						arrayList = new ArrayList();
						string[] array = text.Split(new char[]
						{
							','
						});
						for (int i = 0; i < array.Length; i++)
						{
							string text3 = array[i].ToString();
							if (!arrayList.Contains(text3))
							{
								arrayList.Add(text3);
							}
						}
					}
				}
			}
			return arrayList;
		}
		public void GetDeliverTypeInfoByFashionName(string fashionNameStr, RadioButtonList rblDeliverType)
		{
			int num = 0;
			string text = "";
			if (!string.IsNullOrEmpty(fashionNameStr))
			{
				string[] array = fashionNameStr.Split(new char[]
				{
					'|'
				});
				if (array.Length > 1)
				{
					num = Convert.ToInt32(array[0]);
					text = array[1].ToString();
				}
				DataTable tableType = this.GetTableType(num);
				rblDeliverType.Items.Clear();
				if (tableType != null)
				{
					if (tableType.Rows.Count > 0)
					{
						foreach (DataRow dataRow in tableType.Rows)
						{
							string text2 = "";
							decimal num2 = 0m;
							decimal num3 = 0m;
							string text3 = "";
							if (dataRow["name"] != DBNull.Value)
							{
								string text4 = dataRow["name"].ToString();
								if (text4.Trim().Equals(text.Trim()))
								{
									if (dataRow["TypeName"] != DBNull.Value)
									{
										text2 = dataRow["TypeName"].ToString();
									}
									if (dataRow["default"] != DBNull.Value)
									{
										num2 = Convert.ToDecimal(dataRow["default"].ToString());
									}
									if (dataRow["defIncrement"] != DBNull.Value)
									{
										num3 = Convert.ToDecimal(dataRow["defIncrement"].ToString());
									}
									if (dataRow["area"] != DBNull.Value)
									{
										text3 = dataRow["area"].ToString();
									}
									ListItem listItem = new ListItem();
									listItem.Text = string.Concat(new object[]
									{
										text2,
										"——",
										text4,
										"  ",
										num2,
										"元每加一件加",
										num3,
										"元 至 ",
										text3
									});
									listItem.Value = this.SetDeliverChargeContentDetail(num, text4, text3, num2, num3);
									rblDeliverType.Items.Add(listItem);
								}
							}
						}
					}
				}
			}
			if (rblDeliverType.Items.Count > 0)
			{
				rblDeliverType.Items[0].Selected = true;
			}
		}
		public void SelectDeliverItemByProvince(string deliverPayType, string m_selProvoince, RadioButtonList rblDeliverType)
		{
			if (!string.IsNullOrEmpty(m_selProvoince))
			{
				if (deliverPayType != null)
				{
					if (deliverPayType == "0")
					{
						goto IL_245;
					}
				}
				for (int i = 0; i < rblDeliverType.Items.Count; i++)
				{
					rblDeliverType.Items[i].Selected = false;
				}
				for (int i = 0; i < rblDeliverType.Items.Count; i++)
				{
					string value = rblDeliverType.Items[i].Value;
					bool flag = false;
					if (!string.IsNullOrEmpty(value))
					{
						string[] contentArray = value.ToString().Split(new char[]
						{
							'$'
						});
						string deliverChargeContentDetailByName = Shop_DeliverType.GetDeliverChargeContentDetailByName("arriveArea", contentArray);
						if (!string.IsNullOrEmpty(deliverChargeContentDetailByName))
						{
							string[] array = deliverChargeContentDetailByName.Split(new char[]
							{
								','
							});
							for (int j = 0; j < array.Length; j++)
							{
								if (m_selProvoince.Contains(array[j].ToString()))
								{
									rblDeliverType.Items[i].Selected = true;
									flag = true;
									break;
								}
							}
						}
						if (flag)
						{
							break;
						}
						rblDeliverType.Items[i].Selected = false;
					}
				}
				bool flag2 = false;
				for (int i = 0; i < rblDeliverType.Items.Count; i++)
				{
					if (rblDeliverType.Items[i].Selected)
					{
						flag2 = true;
					}
				}
				if (!flag2)
				{
					for (int i = 0; i < rblDeliverType.Items.Count; i++)
					{
						string value = rblDeliverType.Items[i].Value;
						if (!string.IsNullOrEmpty(value))
						{
							string[] contentArray = value.ToString().Split(new char[]
							{
								'$'
							});
							string deliverChargeContentDetailByName = Shop_DeliverType.GetDeliverChargeContentDetailByName("arriveArea", contentArray);
							if (string.IsNullOrEmpty(deliverChargeContentDetailByName))
							{
								rblDeliverType.Items[i].Selected = true;
								break;
							}
						}
					}
				}
				IL_245:;
			}
		}
	}
}
